﻿#!/bin/bash

# 虚谷统计服务部署脚本
# 使用方法: ./deploy-analytics.sh <服务器IP> <用户名> <部署路径> [端口] [SSH端口]

if [ $# -lt 3 ] || [ $# -gt 5 ]; then
    echo "使用方法: $0 <服务器IP> <用户名> <部署路径> [统计服务端口] [SSH端口]"
    echo "示例: $0 192.168.1.100 root /var/www/xugudb-analytics 3001 2222"
    exit 1
fi

SERVER_IP=$1
USERNAME=$2
DEPLOY_PATH=$3
PORT=${4:-3001}
SSH_PORT=${5:-22}

echo "📊 开始部署虚谷统计服务到服务器..."
echo "📍 服务器: $SERVER_IP"
echo "👤 用户: $USERNAME"
echo "📁 部署路径: $DEPLOY_PATH"

# 1. 检查部署包是否存在
if [ ! -f "xugudb-analytics.tar.gz" ]; then
    echo "❌ 统计服务部署包不存在，请先运行: npm run package:analytics"
    exit 1
fi

echo "📦 部署包大小: $(du -h xugudb-analytics.tar.gz | cut -f1)"

# 2. 上传部署包到服务器
echo "📤 上传部署包到服务器..."
scp -P $SSH_PORT xugudb-analytics.tar.gz $USERNAME@$SERVER_IP:/tmp/

if [ $? -ne 0 ]; then
    echo "❌ 上传失败"
    exit 1
fi

# 3. 在服务器上部署
echo "🔧 在服务器上部署..."
ssh -p $SSH_PORT $USERNAME@$SERVER_IP << EOF
    echo "📦 解压部署包..."
    sudo mkdir -p $DEPLOY_PATH
    sudo tar -xzf /tmp/xugudb-analytics.tar.gz -C $DEPLOY_PATH
    sudo chown -R root:root $DEPLOY_PATH
    
    echo "📦 安装依赖..."
    cd $DEPLOY_PATH
    npm install --production
    
    echo "🔧 设置启动脚本权限..."
    chmod +x start.sh stop.sh restart.sh
    
    # 更新配置文件中的服务器地址
    echo "🔧 更新配置文件..."
    if [ -f "script/analytics-config.json" ]; then
        # 备份原配置文件
        cp script/analytics-config.json script/analytics-config.json.backup
        
        # 更新服务器地址和端口
        sed -i "s/{SERVER_IP}/$SERVER_IP/g" script/analytics-config.json
        sed -i "s/localhost/$SERVER_IP/g" script/analytics-config.json
        sed -i "s/3001/$PORT/g" script/analytics-config.json
        
        echo "✅ 配置文件已更新，服务器地址: $SERVER_IP"
    fi
    
    # 检查数据文件是否存在，如果不存在则创建初始化文件
    echo "🔍 检查数据文件: $DEPLOY_PATH/script/analytics-data.json"
    if [ ! -f "script/analytics-data.json" ]; then
        echo "📄 创建初始数据文件..."
        mkdir -p script
        cat > script/analytics-data.json << 'DATA_EOF'
{
  "stats": {
    "totalVisits": 0,
    "todayVisits": 0,
    "weekVisits": 0,
    "monthVisits": 0
  },
  "popularPages": [],
  "lastUpdated": "",
  "todayDate": "",
  "weekDate": 0,
  "monthDate": "",
  "versions": {
    "latest": {
      "stats": {
        "totalVisits": 0,
        "todayVisits": 0,
        "weekVisits": 0,
        "monthVisits": 0
      },
      "popularPages": [],
      "lastUpdated": "",
      "todayDate": "",
      "weekDate": 0,
      "monthDate": ""
    }
  }
}
DATA_EOF
        echo "✅ 数据文件已创建"
    else
        echo "✅ 数据文件已存在，保留现有数据"
        echo "📄 数据文件大小: \$(ls -lh script/analytics-data.json | awk '{print \$5}')"
    fi
    
    echo "🚀 启动统计服务..."
    ./start.sh
    
    echo "✅ 统计服务部署完成！"
    echo "🌐 服务地址: http://$SERVER_IP:$PORT"
EOF

if [ $? -eq 0 ]; then
    echo "✅ 统计服务部署成功！"
    echo "🌐 服务地址: http://$SERVER_IP:$PORT"
    echo ""
    echo "📋 管理命令:"
    echo "  停止服务: ssh -p $SSH_PORT root@$SERVER_IP 'cd $DEPLOY_PATH && ./stop.sh'"
    echo "  重启服务: ssh -p $SSH_PORT root@$SERVER_IP 'cd $DEPLOY_PATH && ./restart.sh'"
    echo "  查看日志: ssh -p $SSH_PORT root@$SERVER_IP 'tail -f /var/log/xugudb-analytics.log'"
    echo "  检查状态: ssh root@$SERVER_IP 'ps aux | grep analytics-server'"
    echo "  查看数据: ssh root@$SERVER_IP 'ls -la $DEPLOY_PATH/script/'"
    echo "  查看配置: ssh root@$SERVER_IP 'cat $DEPLOY_PATH/script/analytics-config.json'"
else
    echo "❌ 部署失败"
    exit 1
fi 